<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="queryObj.query" placeholder="请输入内容" clearable @clear="getUserList">
            <el-button @click="searchUser" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">
            添加用户
          </el-button>
        </el-col>
      </el-row>
      <!-- 用户列表的表格 -->
      <el-table border :data="userList" @selection-change="handleSelectionChange">
        <!-- 复选框 -->
        <el-table-column type="selection">
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column label="编号" type="index">
        </el-table-column>
        <el-table-column label="姓名" prop="username">
          <template slot-scope="scope">
            <!-- 以html的语法解析 -->
            <span v-html="scope.row.username"></span>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" prop="email">
        </el-table-column>
        <el-table-column label="电话" prop="mobile">
        </el-table-column>
        <el-table-column label="角色" prop="role_name">
        </el-table-column>
        <el-table-column label="状态" prop="mg_state">
          <template slot-scope="scope">
            <!-- admin:超级管理员，不要显示开关进行修改 -->
            <el-switch @change="changeStateHandler(scope.row)" v-model="scope.row.mg_state" v-if="scope.row.id !== 500">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" v-if="scope.row.id !== 500">
            <el-button @click="showEditUserDialog(scope.row)" size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="delUser(scope.row.id)" size="mini" type="danger" icon="el-icon-delete"></el-button>
            <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
              <el-button @click="showSetRoleDialog(scope.row)" size="mini" type="warning"
                icon="el-icon-setting"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryObj.pagenum" :page-sizes="[3, 6, 9, 12]" :page-size="queryObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <el-button @click="delMoreUser" size="small" type="warning">批量删除</el-button>
    </el-card>

    <!-- 添加用户的对话框 -->
    <el-dialog @close="addDialogClosed" title="添加用户" :visible.sync="addDialogVisible" width="50%">
      <!-- 默认插槽 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- 具名插槽 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm">重置</el-button>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser">确定</el-button>
      </span>
    </el-dialog>
    <!-- 添加用户的对话框 -->
    <el-dialog @close="$refs.editFormRef.resetFields()" title="修改用户" :visible.sync="editDialogVisible" width="50%">
      <!-- 默认插槽 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- 具名插槽 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="$refs.editFormRef.resetFields()">重置</el-button>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="editUser">确定</el-button>
      </span>
    </el-dialog>
    <!-- 分配角色的对话框 -->
    <el-dialog @close="rid=''" title="分配角色" :visible.sync="setRoleDialogVisible" width="30%">
      <div>
        <p>当前的用户: {{ userInfo.username }}</p>
        <p>当前的角色: {{ userInfo.role_name }}</p>
        <p>
          分配新角色:
          <el-select v-model="rid" placeholder="请选择" clearable>
            <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id">
            </el-option>
          </el-select>
        </p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRoleDialogVisible = false">取 消</el-button>
        <el-button @click="saveRole" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入业务逻代码
import User from './user.js'
export default {
  ...User
}
</script>

<style lang="scss" scoped></style>